package com.atguigu.flink.watermark;

import com.atguigu.flink.function.WaterSensorMapFunction;
import com.atguigu.flink.pojo.WaterSensor;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.TimerService;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;

/**
 * Created by Smexy on 2023/2/1
 *
 *      水印需要先产生(通过算子 assignTimestampsAndWatermarks)
 *              assignTimestampsAndWatermarks(WatermarkStrategy w): 基于w策略产生水印。
 *              WatermarkStrategy:  水印的产生策略。水印从数据的时间属性中提取，再加工。
 *                      不需要自定义，系统提供了常见的策略。
 *
 *                      forMonotonousTimestamps(): 取数据的时间属性 - 1ms ，生成水印
 *
 *  如何查看水印
 */
public class Demo1_WaterMarkView
{
    public static void main(String[] args) {

       Configuration conf = new Configuration();
       conf.setInteger("rest.port", 3333);
       StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);

       //设置发送频率
        env.getConfig().setAutoWatermarkInterval(2000);

        //创建水印生成策略
        WatermarkStrategy<WaterSensor> watermarkStrategy = WatermarkStrategy
            //产生连续(从小到大，不存在乱序)的水印
            .<WaterSensor>forMonotonousTimestamps()
            .withTimestampAssigner(new SerializableTimestampAssigner<WaterSensor>()
            {
                //从数据中，选择哪个时间属性制作水印
                @Override
                public long extractTimestamp(WaterSensor element, long recordTimestamp) {
                    return element.getTs();
                }
            });

        //非常重要，第一次玩，一定要把并行度设置为1
        env.setParallelism(1);


        env
                   .socketTextStream("hadoop103", 8888)
                   .map(new WaterSensorMapFunction())
                   //生成水印，水印生成后，作为一种特殊的数据随数据向下游流动
                   .assignTimestampsAndWatermarks(watermarkStrategy)
                   .process(new ProcessFunction<WaterSensor, WaterSensor>()
                   {
                       //来一条数据就触发一次
                       @Override
                       public void processElement(WaterSensor value, Context ctx, Collector<WaterSensor> out) throws Exception {
                           //获取时钟服务
                           TimerService timerService = ctx.timerService();

                           System.out.println("当前数据达到时，process算子的水印(主观时间):"+timerService.currentWatermark());
                           System.out.println("当前数据达到时，process算子的客观时间:"+timerService.currentProcessingTime());

                           out.collect(value);

                       }
                   }).startNewChain()
                   .print();


                try {
                            env.execute();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

    }
}
